package tt.dz.service.imp.base;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.base.OmsCommonDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.base.OmsPubWhitelist;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.service.base.PubWhiteListService;
import tt.dz.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class PubWhitelistServiceImpl extends BaseService implements PubWhiteListService{
	@Autowired
	@Qualifier("base")
	private OmsCommonDao<OmsPubWhitelist> omsCommonDaoImpl;
	
	@Override
	public   JsonCommon<?> add(OmsPubWhitelist pubWhitelist) 
	{
		try 
		{
			omsCommonDaoImpl.add(pubWhitelist);
			return this.buildSuccessJsonCommon(pubWhitelist);
		} catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> update(OmsPubWhitelist pubWhitelist) 
	{
		if (pubWhitelist.getPub_white_list_id() == null || 
				pubWhitelist.getPub_white_list_id() <= 0)
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			omsCommonDaoImpl.update(pubWhitelist);
			return this.buildSuccessJsonCommon(pubWhitelist);
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> delete(String id) {
		if (StringUtils.isNumeric(id))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			omsCommonDaoImpl.deleteById(id, OmsPubWhitelist.class);
			return this.buildSuccessJsonCommon("success");
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> findById(String id) {
		if (StringUtils.isNumeric(id))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try 
		{
			OmsPubWhitelist pubWhitelist = new OmsPubWhitelist();
			pubWhitelist.setPub_white_list_id(Long.parseLong(id));
			OmsPubWhitelist op = omsCommonDaoImpl.findRecordByPK(pubWhitelist);
			return this.buildSuccessJsonCommon(op);
		} 
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonForList<?> findPageObjs(OmsCommonParam param, OmsPubWhitelist pubWhitelist) 
	{
		try 
		{
			List<OmsPubWhitelist> lst = omsCommonDaoImpl.pagingFind(param, pubWhitelist);
			int total = 0;
			if (param.isQueryTotal())
			{
				total = omsCommonDaoImpl.queryTotalByObject(pubWhitelist);
			}
			return this.buildSuccessJsonForList(param, lst, total);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

}
